package cn.iocoder.yudao.module.vote.utils;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.module.vote.dal.dataobject.voteconfig.VoteConfigDO;
import cn.iocoder.yudao.module.vote.dal.dataobject.voteuserresult.VoteUserResultDO;
import com.baomidou.mybatisplus.core.toolkit.LambdaUtils;
import com.baomidou.mybatisplus.core.toolkit.support.LambdaMeta;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author guomoving
 * @description
 * @date
 */
public class FieldUtils{

    public static Object getField(Class className, String field,Object clazz) {

        try {
            String method = StrUtil.toCamelCase("get_"+field);
            return className.getMethod(method).invoke(clazz);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;

    }

    public static void main(String[] args) throws NoSuchMethodException {
//        getField(VoteConfigDO.class,"vote_name");
//        VoteUserResultDO voteUserResultDO = new VoteUserResultDO();
//        voteUserResultDO.setGroupName("123");
//        SFunction<VoteUserResultDO,?> sFunction = VoteUserResultDO::getGroupName;
//        SFunction<VoteUserResultDO,?> sFunction1 = new SFunction<VoteUserResultDO, Object>() {
//            @Override
//            public Object apply(VoteUserResultDO voteUserResultDO) {
//                return FieldUtils.getField(VoteUserResultDO.class,"group_name",voteUserResultDO);
//            }
//        };
//        SFunction<VoteUserResultDO,?> sFunction2 = field-> FieldUtils.getField(VoteUserResultDO.class,"group_name",field);
//
//        System.out.println(sFunction.apply(voteUserResultDO));
//        System.out.println(sFunction1.apply(voteUserResultDO));
//        System.out.println(sFunction2.apply(voteUserResultDO));

        SFunction<VoteUserResultDO, String> getAppId = VoteUserResultDO::getGroupName;
        LambdaMeta extract = LambdaUtils.extract(getAppId);
        System.out.println(extract.getImplMethodName());
        SFunction<VoteUserResultDO,?> sFunction2 = field-> {
            try {
                return field.getClass().getMethod("getGroupName").invoke(field);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            return null;
        };



        LambdaMeta extract2 = LambdaUtils.extract(sFunction2);
        System.out.println(extract2.getImplMethodName());
    }
}

